package com.bookmanger.server.controller;

import com.bookmanger.common.result.Result;
import com.bookmanger.pojo.DTO.BookDTO;
import com.bookmanger.pojo.Entity.BookEntity;
import com.bookmanger.server.service.BookManagerService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/book/manager")
@Slf4j
public class BookManagerController {

    @Autowired
    private BookManagerService bookManagerService;

    @PostMapping("/add")
    public Result addBook(@RequestBody BookDTO bookDTO) {
        return bookManagerService.addBook(bookDTO) ? Result.success("添加成功") : Result.error("添加失败");
    }

    @DeleteMapping("/delete/{bookNumber}")
    public Result deleteBook(@PathVariable String bookNumber) {
        return bookManagerService.deleteBookByBookNumber(bookNumber) ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PutMapping("/update")
    public Result updateBook(@RequestBody BookDTO bookDTO) {
        return bookManagerService.updateBook(bookDTO) ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping("/{id}")
    public Result getBook(@PathVariable Integer id) {
        return Result.success(bookManagerService.getBookById(id));
    }

    @GetMapping("/search")
    public Result searchBooks(@RequestParam String keyword,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        // 参数校验，防止不合理参数导致性能问题
        if (page <= 0) page = 1;
        if (pageSize <= 0 || pageSize > 100) pageSize = 10;
        
        PageInfo<BookDTO> books = bookManagerService.searchBooks(keyword, page, pageSize);
        return Result.success(books);
    }
    
    /**
     * 分页获取全部ISBN及其对应信息
     * @param page 页码
     * @param pageSize 每页数量
     * @return 分页结果
     */
    @GetMapping("/getAllBooks")
    public Result getAllBooks(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        // 参数校验，防止不合理参数导致性能问题
        if (page <= 0) page = 1;
        if (pageSize <= 0 || pageSize > 100) pageSize = 10;
        
        PageInfo<BookDTO> books = bookManagerService.getAllBooks(page, pageSize);
        return Result.success(books);
    }
    
    /**
     * 根据ISBN ID获取实体书列表
     * @param isbnId ISBN ID
     * @return 实体书列表
     */
    @GetMapping("/entities/{isbnId}")
    public Result getBookEntitiesByIsbnId(@PathVariable Integer isbnId) {
        List<BookEntity> bookEntities = bookManagerService.getBookEntitiesByIsbnId(isbnId);
        return Result.success(bookEntities);
    }
    
    /**
     * 增加实体书
     * @param isbnId ISBN ID
     * @param quantity 增加数量
     * @return 操作结果
     */
    @PostMapping("/entities/add")
    public Result addBookEntities(@RequestParam Integer isbnId, @RequestParam Integer quantity) {
        if (quantity <= 0) {
            return Result.error("数量必须大于0");
        }
        
        return bookManagerService.addBookEntities(isbnId, quantity) ? 
            Result.success("实体书添加成功") : Result.error("实体书添加失败");
    }
    
    /**
     * 删除实体书
     * @param bookId 实体书ID
     * @return 操作结果
     */
    @DeleteMapping("/entities/{bookId}")
    public Result deleteBookEntity(@PathVariable Integer bookId) {
        return bookManagerService.deleteBookEntity(bookId) ? 
            Result.success("实体书删除成功") : Result.error("实体书删除失败");
    }
    
    /**
     * 根据ISBN ID删除图书及其所有实体书
     * @param id ISBN ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result deleteBookById(@PathVariable Integer id) {
        return bookManagerService.deleteBookById(id) ?
            Result.success("图书删除成功") : Result.error("图书删除失败");
    }
}